package com.yiyang.controller;

import com.yiyang.pojo.Result;
import com.yiyang.pojo.Room;
import com.yiyang.service.RoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource
    private RoomService roomService;

    @GetMapping("/list")
    public Result<List<Room>> getRoomList(
            @RequestParam(name = "currentPage",defaultValue = "1",required = false) Integer currentPage,
            @RequestParam(name = "pageSize",defaultValue = "10" ,required = false) Integer pageSize,
            @RequestParam(name = "name",defaultValue = "",required = false) String name,
            @RequestParam(name = "floor",defaultValue = "",required = false) String floor
    ){
            return roomService.getRoomList(currentPage,pageSize,name,floor);
    }

    @PostMapping("/add")
    public Result<Void> addRoom(@RequestBody Room room){
            return roomService.addRoom(room);
    }

    @PutMapping("/delete")
    public Result<Void> deleteRoom(@RequestParam(name = "id" ,required = true) Integer id){
        return roomService.deleteRoom(id);
    }

    @DeleteMapping("/update")
    public Result<Void> updateRoom(@RequestBody Room room){
        return roomService.updateRoom(room);
    }
}
